//
//  NWNewFeatureViewController.swift
//  Weibo
//
//  Created by Northwan on 16/1/23.
//  Copyright © 2016年 Northwan. All rights reserved.
//

import UIKit

class NWNewFeatureViewController: UIViewController {

    override func viewDidLoad() {
        super.viewDidLoad()
        setupUI()
    }
    
    private func setupUI(){
        scrollView.frame = view.bounds
        view.addSubview(scrollView)
      
        
        let count = 4
        
        for i in 0..<count {
            let imgView = UIImageView(image: UIImage(named: "new_feature_\(i+1)"))
            imgView.frame = CGRect(x: CGFloat(i) * scrollView.width, y: 0, width: scrollView.width, height: scrollView.height)
            scrollView.addSubview(imgView)
            
            if i == count - 1{
                setLastPage(imgView)
            }
        }
        scrollView.contentSize  = CGSize(width: CGFloat(count) * scrollView.width, height: 0)
        
        
        pageControl.centerX = scrollView.centerX
        pageControl.y = scrollView.height - 100
        view.addSubview(pageControl)

        pageControl.numberOfPages = count
        
    }
    
    private func setLastPage(imageView: UIImageView){
        imageView.userInteractionEnabled = true
        enterButton.centerX = imageView.width*0.5
        enterButton.y = imageView.height-150
        imageView.addSubview(enterButton)
        
        shareButton.centerX = imageView.width*0.5
        shareButton.y = CGRectGetMinY(enterButton.frame)-50
        imageView.addSubview(shareButton)
    }
    
    // MARK: - 懒加载
    private lazy var scrollView:UIScrollView = {
        let scrollView = UIScrollView()
        scrollView.delegate = self
        scrollView.pagingEnabled = true
        scrollView.bounces = false
        scrollView.showsHorizontalScrollIndicator = false
        return scrollView
    }()
    
    private lazy var pageControl:UIPageControl = {
        let pageControl = UIPageControl()
        //如果在父控件的区域之外,设置这个属性就会把区域之外的干掉
//        pageControl.clipsToBounds = true
        pageControl.pageIndicatorTintColor = UIColor(white: 1, alpha: 0.8)
        pageControl.currentPageIndicatorTintColor = UIColor.orangeColor()
        return pageControl
    }()
    
    private lazy var enterButton:UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "enterButtonClick", forControlEvents: .TouchUpInside)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: .Normal)
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: .Highlighted)
        button.setTitle("进入微博", forState: .Normal)
        button.setTitleColor(UIColor.whiteColor(), forState: .Normal)
        button.sizeToFit()
        return button
    }()
    
    private lazy var shareButton:UIButton = {
        let button = UIButton()
        button.addTarget(self, action: "shareButtonClick:", forControlEvents: .TouchUpInside)
        button.setImage(UIImage(named: "new_feature_share_false"), forState: .Normal)
        button.setImage(UIImage(named: "new_feature_share_true"), forState: .Selected)
        button.setTitle(" 分享到微博", forState: .Normal)
        button.setTitleColor(UIColor.darkGrayColor(), forState: .Normal)
        button.sizeToFit()
        return button
    }()
    
    // MARK: - 监听事件
    @objc private func enterButtonClick(){
        printLog("进入微博")
        NSNotificationCenter.defaultCenter().postNotificationName(kSwithRootCtrlNortif, object: self)
    }
    
    @objc private func shareButtonClick(sender:UIButton){
        sender.selected = !sender.selected
    }
}

extension NWNewFeatureViewController:UIScrollViewDelegate{
    func scrollViewDidScroll(scrollView: UIScrollView) {
       //+0.5解决左滑Int()去下限的问题，这样就是四舍五入
       let page =  scrollView.contentOffset.x / scrollView.width+0.5
       pageControl.currentPage = Int(page)
    }
}
